#ifndef SKYROAD_CONVERT_TO_H_
#define SKYROAD_CONVERT_TO_H_

#include <string>
#include <cstdlib>

#ifdef _WIN32
  #include <windows.h>
#endif // _WIN32


inline std::string to_string(int i) {
	char buf[64] = {};
	itoa(i, buf, 10);
	return buf;	
}

inline std::string to_string(unsigned __int64 ui) {
	char buf[64] = {};
	_ui64toa(ui, buf, 10);
	return buf;
}

inline std::string to_string(float f) {
	char buf[64] = {};
	sprintf(buf, "%.2f", f);
	return buf;
}

inline int to_int(const std::string& s) {
	return atoi(s.c_str());
}

inline __int64 to_int64(const std::string& s) {
	return _atoi64(s.c_str());
}	

inline float to_float(const std::string& s) {
	return (float)atof(s.c_str());
}

inline double to_double(const std::string& s) {
	return atof(s.c_str());
}

inline unsigned long to_ulong(const std::string& s) {
	return atol(s.c_str());
}

inline long to_long(const std::string& s) {
	return atol(s.c_str());
}

#ifdef _WIN32

static std::string unicode_to_native(const std::wstring& text) {
	std::string abuf;
	if (text.size() == 0)
		return abuf;
	abuf.resize(text.size()*2);
	int code = WideCharToMultiByte(
		CP_ACP,			// code page
		0,				// performance and mapping flags
		&text[0],		// wide-character string
		text.length(),	// number of chars in string
		&abuf[0],		// buffer for new string
		abuf.size(),	// size of buffer
		0,				// default for unmappable chars
		0				// set when default char used
	);

	if (code > 0)
		abuf.resize(code);
	return abuf;
}


static std::wstring native_to_unicode(const std::string& text) {
	std::wstring wbuf;
	if (text.size() == 0) 
		return wbuf;
	wbuf.resize(text.size());
	int code = MultiByteToWideChar(
		CP_ACP,			// code page
		MB_PRECOMPOSED,	// character-type options
		text.c_str(),	// string to map
		text.size(),	// number of bytes in string
		&wbuf[0],		// wide-character buffer
		wbuf.size()		// size of buffer
	);

	if (code > 0)
		wbuf.resize(code);
	return wbuf;
}

static std::wstring utf8_to_unicode(const std::string& text) {
	std::wstring wbuf;
	if (text.size() == 0)
		return wbuf;
	wbuf.resize(text.size()*2);
	int code = MultiByteToWideChar(
		CP_UTF8,		// code page
		0/*MB_PRECOMPOSED*/,	// character-type options
		text.c_str(),	// string to map
		text.size(),	// number of bytes in string
		&wbuf[0],		// wide-character buffer
		wbuf.size()		// size of buffer
	);

	if (code > 0)
		wbuf.resize(code);
	return wbuf;
}
#endif

#endif